home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / TempoController.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  5.6 KB  |  167 lines  |  [TEXT/KAHL]

  1. /* TempoController.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "TempoController.h"
  31. #include "Memory.h"
  32. #include "LinearTransition.h"
  33. #include "PlayTrackInfoThang.h"
  34.  
  35.  
  36. #define MINBPM (Double2LargeBCD(1))
  37.  
  38.  
  39. struct TempoControlRec
  40.     {
  41.         LargeBCDType                        DefaultBeatsPerMinute;
  42.         LargeBCDType                        CurrentBeatsPerMinute;
  43.         struct LinearTransRec*    BeatsPerMinuteChange;
  44.         long                                        BeatsPerMinuteChangeCountdown;
  45.     };
  46.  
  47.  
  48. /* create a new tempo control record */
  49. TempoControlRec*                NewTempoControl(LargeBCDType DefaultBeatsPerMinute)
  50.     {
  51.         TempoControlRec*            Tempo;
  52.  
  53.         Tempo = (TempoControlRec*)AllocPtrCanFail(sizeof(TempoControlRec),"TempoControlRec");
  54.         if (Tempo == NIL)
  55.             {
  56.              FailurePoint1:
  57.                 return NIL;
  58.             }
  59.         Tempo->DefaultBeatsPerMinute = DefaultBeatsPerMinute;
  60.         Tempo->CurrentBeatsPerMinute = Tempo->DefaultBeatsPerMinute;
  61.         Tempo->BeatsPerMinuteChange = NewLinearTransition(0,0,1);
  62.         if (Tempo->BeatsPerMinuteChange == NIL)
  63.             {
  64.              FailurePoint2:
  65.                 ReleasePtr((char*)Tempo);
  66.                 goto FailurePoint1;
  67.             }
  68.         Tempo->BeatsPerMinuteChangeCountdown = 0;
  69.         return Tempo;
  70.     }
  71.  
  72.  
  73. /* dispose of a tempo control record */
  74. void                                        DisposeTempoControl(TempoControlRec* Tempo)
  75.     {
  76.         CheckPtrExistence(Tempo);
  77.         DisposeLinearTransition(Tempo->BeatsPerMinuteChange);
  78.         ReleasePtr((char*)Tempo);
  79.     }
  80.  
  81.  
  82. /* update the tempo control & return the new value */
  83. LargeBCDType                        TempoControlUpdate(TempoControlRec* Tempo, long NumTicks)
  84.     {
  85.         CheckPtrExistence(Tempo);
  86.         if (NumTicks < Tempo->BeatsPerMinuteChangeCountdown)
  87.             {
  88.                 Tempo->CurrentBeatsPerMinute = LinearTransitionUpdateMultiple(
  89.                     Tempo->BeatsPerMinuteChange,NumTicks);
  90.                 Tempo->BeatsPerMinuteChangeCountdown -= NumTicks;
  91.             }
  92.         else if (Tempo->BeatsPerMinuteChangeCountdown > 0)
  93.             {
  94.                 Tempo->CurrentBeatsPerMinute = LinearTransitionUpdateMultiple(
  95.                     Tempo->BeatsPerMinuteChange,Tempo->BeatsPerMinuteChangeCountdown);
  96.                 Tempo->BeatsPerMinuteChangeCountdown = 0;
  97.             }
  98.         return Tempo->CurrentBeatsPerMinute;
  99.     }
  100.  
  101.  
  102. /* reset tempo to default value */
  103. void                                        TempoControlRestoreDefault(TempoControlRec* Tempo)
  104.     {
  105.         CheckPtrExistence(Tempo);
  106.         Tempo->CurrentBeatsPerMinute = Tempo->DefaultBeatsPerMinute;
  107.         Tempo->BeatsPerMinuteChangeCountdown = 0;
  108.     }
  109.  
  110.  
  111. /* set the tempo to the specified number of beats per minute */
  112. void                                        TempoControlSetBeatsPerMinute(TempoControlRec* Tempo,
  113.                                                     LargeBCDType NewBeatsPerMinute)
  114.     {
  115.         CheckPtrExistence(Tempo);
  116.         Tempo->CurrentBeatsPerMinute = NewBeatsPerMinute;
  117.         if (Tempo->CurrentBeatsPerMinute < MINBPM)
  118.             {
  119.                 Tempo->CurrentBeatsPerMinute = MINBPM;
  120.             }
  121.         Tempo->BeatsPerMinuteChangeCountdown = 0;
  122.     }
  123.  
  124.  
  125. /* adjust the tempo by adding the specified value to it */
  126. void                                        TempoControlAdjustBeatsPerMinute(TempoControlRec* Tempo,
  127.                                                     LargeBCDType IncrementBeatsPerMinute)
  128.     {
  129.         CheckPtrExistence(Tempo);
  130.         Tempo->CurrentBeatsPerMinute += IncrementBeatsPerMinute;
  131.         if (Tempo->CurrentBeatsPerMinute < MINBPM)
  132.             {
  133.                 Tempo->CurrentBeatsPerMinute = MINBPM;
  134.             }
  135.         Tempo->BeatsPerMinuteChangeCountdown = 0;
  136.     }
  137.  
  138.  
  139. /* sweep the tempo to a new value */
  140. void                                        TempoControlSweepToNewValue(TempoControlRec* Tempo,
  141.                                                     LargeBCDType NewBPM, SmallExtBCDType NumBeatsToReach)
  142.     {
  143.         CheckPtrExistence(Tempo);
  144.         if (NewBPM < MINBPM)
  145.             {
  146.                 NewBPM = MINBPM;
  147.             }
  148.         if (NumBeatsToReach < 0)
  149.             {
  150.                 NumBeatsToReach = 0;
  151.             }
  152.         Tempo->BeatsPerMinuteChangeCountdown = SmallExtBCD2Double(NumBeatsToReach)
  153.             * (DURATIONUPDATECLOCKRESOLUTION / 4);
  154.         RefillLinearTransition(Tempo->BeatsPerMinuteChange,Tempo->CurrentBeatsPerMinute,
  155.             NewBPM,Tempo->BeatsPerMinuteChangeCountdown);
  156.     }
  157.  
  158.  
  159. /* sweep the tempo to a new value relative to the current value */
  160. void                                        TempoControlSweepToAdjustedValue(TempoControlRec* Tempo,
  161.                                                     LargeBCDType AdjustBPM, SmallExtBCDType NumBeatsToReach)
  162.     {
  163.         CheckPtrExistence(Tempo);
  164.         TempoControlSweepToNewValue(Tempo,Tempo->CurrentBeatsPerMinute + AdjustBPM,
  165.             NumBeatsToReach);
  166.     }
  167.